package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.exception.CustomException;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangyan
 * @date 2021/2/21 9:56
 */
@Service
@Transactional //类中所有方法都有事务
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    //根据id查询
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) //只读的事务
    public User findById(Integer id) {
        User user = userDao.findById(id);
        if(user == null){
            throw new CustomException("该用户信息不存在!");
        }
        return user;
    }

    //新增
    @Override
    public void add(User user) {
        userDao.add(user);
    }

    //查询所有
    @Override
    public List<User> findAll() {
        List<User> userList = userDao.findAll();
        return userList;
    }

    //根据id删除
    @Override
    public void delete(Integer id) {
        userDao.delete(id);
    }

    //修改
    @Override
    public void update(User user) {
        userDao.update(user);
    }
}
